using System;
using System.Collections.Generic;
using Game.Network;

namespace Game.Network {
    public abstract class TCPPacket: Packet {
        protected PacketWriter m_Stream;
        private int m_packetID;
        protected bool m_setLength;
    
        public int Length{ get { return (int)m_Stream.Length; } }
    
        protected TCPPacket (int packetID) {
            m_packetID = packetID;
            m_setLength = true;
        }
    
        protected TCPPacket (int packetID, int length) {
            m_packetID = packetID;
            m_Stream = new PacketWriter (length);
            m_Stream.Write ((byte)packetID);
            m_setLength = false;
        }
                
        protected void EnsureCapacity (int length) {
            m_Stream = new PacketWriter (length);
            m_Stream.Write ((byte)m_packetID);
            m_Stream.Write ((short)0);
        }
    
        public override byte[] Compile () {
            if (m_setLength) {
                m_Stream.Seek (1, System.IO.SeekOrigin.Begin);
                m_Stream.Write ((ushort)m_Stream.Length);
            }
            return m_Stream.ToArray ();
        }
    }
}